#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

bool compare(pair<string, int> lhs, pair<string, int> rhs) { return lhs.second > rhs.second; }

int main() {
  unordered_map<string, int> counter{{"hello", 2}, {"world", 3}, {"to", 5}, {"of", 10}, {"good", 1}};

  cout << "======== before sort:\n";
  for (auto &&item : counter) {
    cout << item.first << " : " << item.second << "\n";
  }

  // map 以pair的形式存放在vector中，然后对vector排序
  vector<pair<string, int>> vec{counter.begin(), counter.end()};
  sort(vec.begin(), vec.end(), compare);

  cout << "======== after sort:\n";
  for (auto &&item : vec) {
    cout << item.first << " : " << item.second << "\n";
  }
  return 0;
}